//
//  secondViewController.m
//  HumorousSript
//
//  Created by 李欢 on 15/6/25.
//  Copyright (c) 2015年 李欢. All rights reserved.
//

#import "secondViewController.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "RecTableViewCell.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height


@interface secondViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray * tableArray;
@property (nonatomic, retain) UITableView * tableView;
@end

@implementation secondViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
             [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    3件没做过的事
    
    [SANetWorkingTask request:@"http://ic.snssdk.com/neihan/stream/category/data/v2/?tag=joke&iid=2808558815&message_cursor=0&level=6&category_id=127&count=30&ac=WIFI&channel=tongbu&app_name=joke_essay&aid=7&version_code=4.1.0&device_platform=iphone&os_version=8.3&device_type=iPhone%205S&device_id=4272329077&vid=B06E994A-E093-479F-99E0-351D9340559F&openudid=e00e3962e37339ec7ce09708cad23b19b7a0693b&os_api=18&idfa=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&screen_width=640" parmater:nil block:^(NSDictionary *result) {
        NSMutableDictionary * dic = [result objectForKey:@"data"];
        NSMutableArray * array = [dic objectForKey:@"data"];
        self.tableArray = [NSMutableArray arrayWithArray:array];
        
        [self creatView];
        [self.tableView reloadData];
    }];
    
}

-(void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height - 64/667.0*Height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    重定池
    static NSString * cellIdentify = @"cell";
    RecTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[RecTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }
    NSMutableDictionary * Dic = [self.tableArray objectAtIndex:indexPath.row];
    NSMutableDictionary * dic = [Dic objectForKey:@"group"];
    NSMutableDictionary * uDIc = [dic objectForKey:@"user"];
    [cell.imageview sd_setImageWithURL:[uDIc objectForKey:@"avatar_url"] placeholderImage:nil];
    cell.nameLabel.text = [uDIc objectForKey:@"name"];
    
    
    
    NSDictionary *dic5 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [[dic objectForKey:@"content"] boundingRectWithSize:CGSizeMake(335, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic5 context:nil];
    cell.wordsLabel.frame = CGRectMake(20/375.0*Width, 50/667.0*Height, 300/375.0*Width, rect.size.height);
    cell.wordsLabel.text = [dic objectForKey:@"content"];
    NSMutableDictionary * lDic = [dic objectForKey:@"large_image"];
    
    
    cell.y = [[lDic objectForKey:@"height"]integerValue];
    NSMutableArray * arr = [lDic objectForKey:@"url_list"];
    NSMutableDictionary * dica = [arr objectAtIndex:0];
    [cell.picView sd_setImageWithURL:[dica objectForKey:@"url"] placeholderImage:nil];
    
    
    
    [cell setAlabelFrame];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSMutableDictionary * Dica = [self.tableArray objectAtIndex:indexPath.row];
    NSMutableDictionary * dica = [Dica objectForKey:@"group"];
     NSMutableDictionary * lDic = [dica objectForKey:@"large_image"];
    
    
    
    NSDictionary *dic5 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [[dica objectForKey:@"content"] boundingRectWithSize:CGSizeMake(335, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic5 context:nil];

    return  (rect.size.height +  [[lDic objectForKey:@"height"] integerValue] + 80)/667.0*Height;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
